package 二维码.google;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

public class QRCodeGenerator { //generrator -> 生成器

    private static final String QR_CODE_IMAGE_PATH = "C:\\Users\\mfg\\Desktop/二维码.png";


    public static void generatorQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException, WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        //BarcodeFormat -> 条形码   bitMatrix -> 位矩阵
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE,width,height);
        //获取地址
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix,"png",path);
    }

    public static void main(String[] args) {
        try {
            generatorQRCodeImage("Willy, What are you doing",350,350,QR_CODE_IMAGE_PATH);
        } catch (WriterException e) {
            System.out.println("不能生成二维码：" + e.getMessage());
        } catch (IOException e) {
            System.out.println("Could not generate QR Code," + e.getMessage());
        }

    }
}
